package com.gxa.jd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.entity.PageResult;
import com.gxa.jd.mapper.GoodsOrderShippingMapper;
import com.gxa.jd.pojo.GoodsOrderShipping;
import com.gxa.jd.service.GoodsOrderShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class GoodsOrderShippingServiceImpl implements GoodsOrderShippingService {

    @Autowired
    private GoodsOrderShippingMapper goodsOrderShippingMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<GoodsOrderShipping> findAll() {
        return goodsOrderShippingMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<GoodsOrderShipping> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<GoodsOrderShipping> orderShippings = (Page<GoodsOrderShipping>) goodsOrderShippingMapper.selectAll();
        return new PageResult<GoodsOrderShipping>(orderShippings.getTotal(),orderShippings.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<GoodsOrderShipping> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return goodsOrderShippingMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<GoodsOrderShipping> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<GoodsOrderShipping> orderShippings = (Page<GoodsOrderShipping>) goodsOrderShippingMapper.selectByExample(example);
        return new PageResult<GoodsOrderShipping>(orderShippings.getTotal(),orderShippings.getResult());
    }

    /**
     * 根据Id查询
     * @param
     * @return
     */
    public GoodsOrderShipping findById(String orderId) {
        return goodsOrderShippingMapper.selectByPrimaryKey(orderId);
    }

    /**
     * 新增
     * @param
     */
    public void add(GoodsOrderShipping goodsOrderShipping) {
        goodsOrderShippingMapper.insert(goodsOrderShipping);
    }

    /**
     * 修改
     * @param
     */
    public void update(GoodsOrderShipping goodsOrderShipping) {
        goodsOrderShippingMapper.updateByPrimaryKeySelective(goodsOrderShipping);
    }

    /**
     *  删除
     * @param
     */
    public void delete(String orderId) {
        goodsOrderShippingMapper.deleteByPrimaryKey(orderId);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(GoodsOrderShipping.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // order_id
            if(searchMap.get("orderId")!=null && !"".equals(searchMap.get("orderId"))){
                criteria.andLike("orderId","%"+searchMap.get("orderId")+"%");
            }
            // 收货人地址
            if(searchMap.get("receiverAddress")!=null && !"".equals(searchMap.get("receiverAddress"))){
                criteria.andLike("receiverAddress","%"+searchMap.get("receiverAddress")+"%");
            }
            // 城市
            if(searchMap.get("receiverCity")!=null && !"".equals(searchMap.get("receiverCity"))){
                criteria.andLike("receiverCity","%"+searchMap.get("receiverCity")+"%");
            }
            // 收货人名字
            if(searchMap.get("receiverName")!=null && !"".equals(searchMap.get("receiverName"))){
                criteria.andLike("receiverName","%"+searchMap.get("receiverName")+"%");
            }
            // 收货人电话
            if(searchMap.get("receiverPhone")!=null && !"".equals(searchMap.get("receiverPhone"))){
                criteria.andLike("receiverPhone","%"+searchMap.get("receiverPhone")+"%");
            }
            // 省
            if(searchMap.get("receiverProvince")!=null && !"".equals(searchMap.get("receiverProvince"))){
                criteria.andLike("receiverProvince","%"+searchMap.get("receiverProvince")+"%");
            }
            // 镇
            if(searchMap.get("receiverTown")!=null && !"".equals(searchMap.get("receiverTown"))){
                criteria.andLike("receiverTown","%"+searchMap.get("receiverTown")+"%");
            }


        }
        return example;
    }

}
